package main

import (
	"flag"
	"fmt"
	"gitee.com/general252/ffmpeg-tool/handle"
	"gitee.com/general252/ffmpeg-tool/help"
	"gitee.com/general252/ffmpeg-tool/static"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"os"
	"time"
)

var port int

func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func main() {
	time.Duration(22).String()

	flag.IntVar(&port, "port", 8000, "http listen port")
	flag.Parse()

	_ = os.Mkdir("./uploads", os.ModePerm)

	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()

	r.StaticFS("/home", static.GetFileSystem()) // 静态文件
	r.GET("", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "/home")
	})

	home := handle.NewHomeHandle()
	r.POST("/upload", home.Upload)

	audio := handle.NewAudioHandle()
	r.GET("/audio", audio.Convert)

	video := handle.NewVideoHandle()
	r.GET("/video", video.Convert)

	// 打开浏览器
	if true {
		go func() {
			uri := fmt.Sprintf("http://127.0.0.1:%v", port)
			if err := help.OpenBrowsers(uri); err != nil {
				log.Println(err)
			}
		}()
	}

	addr := fmt.Sprintf(":%v", port)
	log.Printf("HTTP listen on %v", addr)

	if err := r.Run(addr); err != nil {
		log.Println(err)
	}
}
